package com.one.common.receiver;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import com.one.common.R;
import com.one.common.utils.Logger;

import androidx.core.app.NotificationCompat;

import static androidx.core.app.NotificationCompat.PRIORITY_DEFAULT;
import static androidx.core.app.NotificationCompat.VISIBILITY_SECRET;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2019/3/28 20:31
 * Description：通知栏常驻通知
 **/
public class NotificationHandle extends ContextWrapper {
    private NotificationManager mManager;
    public static final String CHANNEL_ID = "order channel";
    private static final String CHANNEL_NAME = "订单消息";
    public static final String CHANNEL_GROUP_ID = "order group";
    private static final String CHANNEL_GROUP_NAME = "消息通知";
    public static final int NOTIFICATION_ID = 999;

    public NotificationHandle(Context base) {
        super(base);
        createNotificationChannel();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getManager().createNotificationChannelGroup(new NotificationChannelGroup(CHANNEL_GROUP_ID, CHANNEL_GROUP_NAME));
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            //是否绕过请勿打扰模式
            channel.canBypassDnd();
            //闪光灯
            channel.enableLights(false);
            //锁屏显示通知
            channel.setLockscreenVisibility(VISIBILITY_SECRET);
            //闪关灯的灯光颜色
            channel.setLightColor(Color.RED);
            //桌面launcher的消息角标
            channel.canShowBadge();
            //是否允许震动
            channel.enableVibration(false);
            channel.setGroup(CHANNEL_GROUP_ID);
            //获取通知取到组
            channel.getGroup();
            //设置可绕过  请勿打扰模式
            channel.setBypassDnd(true);

            getManager().createNotificationChannel(channel);
        }


    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public Notification crate(String title, String content, PendingIntent intent) {
        NotificationCompat.Builder builder = getNotification(title, content, intent);

        return builder.build();
    }

    private NotificationCompat.Builder getNotification(String title, String content, PendingIntent intent) {
        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(getApplicationContext());
        }

        builder.setPriority(PRIORITY_DEFAULT);
        //  设置这个标志当用户单击面板就可以让通知将自动取消
        builder.setAutoCancel(false);
        // 设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)
        // 或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
        builder.setOngoing(true);
        // 时间
        builder.setWhen(System.currentTimeMillis());
        //标题
        builder.setContentTitle(title);
        //文本内容

        builder.setContentText(content);
        //小图标
        builder.setSmallIcon(R.drawable.ic_logo);
        // 大图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_logo_car));

        //设置点击信息后的跳转（意图）
        builder.setContentIntent(intent);

        return builder;
    }

    public void notify(String title, String content, PendingIntent intent) {
        Logger.e("显示通知  ");
        getManager().notify(NOTIFICATION_ID, crate(title, content, intent));
    }


}
